package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.IntentKey;
import com.taptech.doufu.constant.SharedPreferencesKey;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.theme.read.ReadThemeHelper;
import com.taptech.doufu.ui.view.theme.ThemeType;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;
import com.taptech.doufu.ui.view.colorful.Colorful;

/**
 * Created by Wyatt on 2017/11/26/026.
 */

public class ReadAnimationActivity extends DiaobaoBaseActivity implements View.OnClickListener {

    private LinearLayout llNone, llLikeBook, llHorizontalCover, llVerticalScroll;
    private ImageView ivSelectNone, ivSelectBook, ivSelectHorizontalCover, ivSelectVerticalScroll;
    private int type;
    private int oldType;
    private Colorful colorful;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setColorful();
        setContentView(R.layout.activity_read_animation);
        initUI();
        type = DiaoBaoSharedPreferences.getSharedPreferencesValueToInt(SharedPreferencesKey.SP_READ_PAGE_READ_ANIMATION,
                this, Constant.ReadAnimationType.VerticalScroll);
        oldType = type;
        setSelectIcon();
    }

    private void setColorful() {
        colorful = new Colorful.Builder(this).create();
        if (ReadThemeHelper.getReadTheme().getThemeType() == ThemeType.TYPE_NIGHT) {
            colorful.setTheme(R.style.theme_dark);
        } else {
            colorful.setTheme(R.style.theme_day);
        }
    }

    @Override
    public void finish() {
        if (oldType != type) {
            Intent intent = new Intent();
            intent.putExtra(IntentKey.INTENT_KEY_TURN_PAGE_ANIMATION, type);
            setResult(RESULT_OK, intent);
        }
        super.finish();
    }

    private void setSelectIcon() {
        if (type == Constant.ReadAnimationType.NONE) {
            ivSelectNone.setVisibility(View.VISIBLE);
            ivSelectBook.setVisibility(View.GONE);
            ivSelectHorizontalCover.setVisibility(View.GONE);
            ivSelectVerticalScroll.setVisibility(View.GONE);
        } else if (type == Constant.ReadAnimationType.LikeBook) {
            ivSelectNone.setVisibility(View.GONE);
            ivSelectBook.setVisibility(View.VISIBLE);
            ivSelectHorizontalCover.setVisibility(View.GONE);
            ivSelectVerticalScroll.setVisibility(View.GONE);
        } else if (type == Constant.ReadAnimationType.HorizontalCover) {
            ivSelectNone.setVisibility(View.GONE);
            ivSelectBook.setVisibility(View.GONE);
            ivSelectHorizontalCover.setVisibility(View.VISIBLE);
            ivSelectVerticalScroll.setVisibility(View.GONE);
        } else {
            ivSelectNone.setVisibility(View.GONE);
            ivSelectBook.setVisibility(View.GONE);
            ivSelectHorizontalCover.setVisibility(View.GONE);
            ivSelectVerticalScroll.setVisibility(View.VISIBLE);
        }
    }

    private void initUI() {
        llNone = (LinearLayout) findViewById(R.id.llNone);
        llNone.setVisibility(View.GONE);
        llLikeBook = (LinearLayout) findViewById(R.id.llLikeBook);
        llLikeBook.setVisibility(View.GONE);
        llHorizontalCover = (LinearLayout) findViewById(R.id.llHorizontalCover);
        llVerticalScroll = (LinearLayout) findViewById(R.id.llVerticalScroll);
        ivSelectNone = (ImageView) findViewById(R.id.ivSelectNone);
        ivSelectBook = (ImageView) findViewById(R.id.ivSelectBook);
        ivSelectHorizontalCover = (ImageView) findViewById(R.id.ivSelectHorizontalCover);
        ivSelectVerticalScroll = (ImageView) findViewById(R.id.ivSelectVerticalScroll);
        llNone.setOnClickListener(this);
        llLikeBook.setOnClickListener(this);
        llHorizontalCover.setOnClickListener(this);
        llVerticalScroll.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llNone:
                if (type == Constant.ReadAnimationType.NONE) {
                    return;
                }
                type = Constant.ReadAnimationType.NONE;
                setSelectIcon();
                break;
            case R.id.llLikeBook:
                if (type == Constant.ReadAnimationType.LikeBook) {
                    return;
                }
                type = Constant.ReadAnimationType.LikeBook;
                setSelectIcon();
                break;
            case R.id.llHorizontalCover:
                if (type == Constant.ReadAnimationType.HorizontalCover) {
                    return;
                }
                type = Constant.ReadAnimationType.HorizontalCover;
                setSelectIcon();
                break;
            case R.id.llVerticalScroll:
                if (type == Constant.ReadAnimationType.VerticalScroll) {
                    return;
                }
                type = Constant.ReadAnimationType.VerticalScroll;
                setSelectIcon();
                break;
        }
    }
}
